package org.tio.sitexxx.web.server.controller.tiosite;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.HttpRequest;
import org.tio.http.common.HttpResponse;
import org.tio.http.server.annotation.RequestPath;
import org.tio.http.server.util.Resps;
import org.tio.sitexxx.service.model.main.Donate;
import org.tio.sitexxx.service.service.tiosite.DonateService;
import org.tio.utils.SystemTimer;
import org.tio.utils.hutool.BetweenFormater;
import org.tio.utils.hutool.BetweenFormater.Level;
import org.tio.utils.resp.Resp;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * @author tanyaowu
 * 2016年7月22日 上午10:44:13
 */
@RequestPath(value = "/donate")
public class DonateController {
	@SuppressWarnings("unused")
	private static Logger log = LoggerFactory.getLogger(DonateController.class);

	/**
	 *
	 * @author tanyaowu
	 */
	public DonateController() {
	}

	@RequestPath(value = "/page")
	public Resp page(Integer pageNumber, Integer pageSize, HttpRequest request) throws Exception {
		Page<Donate> page = DonateService.me.page(pageNumber, pageSize);
		return Resp.ok(page);
	}

	@RequestPath(value = "/totalAmount")
	public Resp totalAmount(HttpRequest request) throws Exception {
		Double totalAmount = DonateService.me.totalAmount();
		return Resp.ok(totalAmount);
	}

	/**
	 * 跳转到支付宝捐赠
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/toAli")
	public HttpResponse toAli(HttpRequest request) throws Exception {
		return Resps.redirect(request, "HTTPS://QR.ALIPAY.COM/FKX04368JZNXDCD9BTIZ5C");
	}

	/**
	 * 跳转到微信捐赠
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestPath(value = "/toWechat")
	public HttpResponse toWechat(HttpRequest request) throws Exception {
		return Resps.redirect(request, "https://wx.tenpay.com/f2f?t=AQAAAP KWmulNp6ZN4Zr0xfN7jY=");
	}

	/**
	 * count | sum                  | waytype | way
	 * 27	 |  804.31           	|  1	  |  码云
	   21	 |  1068                |  2	  |  微信二维码收款
	   15	 |  1409.43	            |  3	  |  支付宝
	   3	 |  214.88	            |  4	  |  微信红包
	 * 统计捐赠来源
	 * @param request
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@RequestPath(value = "/statAmountComeFrom")
	public Resp statAmountComeFrom(HttpRequest request) throws Exception {
		List<Record> list = DonateService.me.statAmountComeFrom();
		return Resp.ok(list);
	}

	/**
	 * count | sum                  | waytype | way
	 * 27	 |  804.31           	|  1	  |  码云
	   21	 |  1068                |  2	  |  微信二维码收款
	   15	 |  1409.43	            |  3	  |  支付宝
	   3	 |  214.88	            |  4	  |  微信红包
	 * 统计捐赠来源次数
	 * @param request
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@RequestPath(value = "/statCountComeFrom")
	public Resp statCountComeFrom(HttpRequest request) throws Exception {
		List<Record> list = DonateService.me.statCountComeFrom();
		return Resp.ok(list);
	}

	/**
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@RequestPath(value = "/orderAmountByName")
	public Resp orderAmountByName(HttpRequest request) throws Exception {
		List<Record> list = DonateService.me.orderAmountByName();
		return Resp.ok(list);
	}

	@RequestPath(value = "/orderCountByName")
	public Resp orderCountByName(HttpRequest request) throws Exception {
		List<Record> list = DonateService.me.orderCountByName();
		return Resp.ok(list);
	}

	/**
	 * 找出最后一笔捐赠时间
	 * @param request
	 * @return
	 * @throws Exception
	 * @author tanyaowu
	 */
	@RequestPath(value = "/selectLastDonate")
	public Resp selectLastDonate(HttpRequest request) throws Exception {
		Record record = DonateService.me.selectLastDonate();
		Date date = record.getDate("time");

		long duration = SystemTimer.currTime - date.getTime();//.firstAccessTime;
		BetweenFormater betweenFormater = new BetweenFormater(duration, Level.SECOND);

		record.set("fromatedDuration", betweenFormater.format());
		record.set("duration", duration);
		return Resp.ok(record);
	}

}
